package cn.com.cetccst.distribution.app;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

import java.util.ArrayList;
import java.util.List;

import cn.com.cetccst.tools.log.CommonLogger;
import cn.com.cetccst.tools.pm.AppPackage;

public class AppInfoManager {


    private static AppInfoManager instance;

    private AppInfoManager(Context context) {
    }

    /**
     * @param context
     */
    public static void initialize(Context context) {

        if (instance == null)
            instance = new AppInfoManager(context);
    }

    public static AppInfoManager getInstance() {
        return instance;
    }

    public List<AppInfo> getInstalledApps(Context context) {
        List<AppInfo> appList = new ArrayList<>();
        List<ApplicationInfo> appInfos = context.getPackageManager().getInstalledApplications(0);
        //TODO 为什么弄了一个小于2，不明所以，先把之前的逻辑抄过来吧
        if (appInfos == null || appInfos.size() < 2) {
            List<ApplicationInfo> appInfosNative = new ArrayList<>();
            appInfosNative.addAll(AppPackage.getInstalledApps(context));
        }

        for (ApplicationInfo info : appInfos) {
            try {
                boolean isSystemApp = AppPackage.isSystemPackage(context, info.packageName);
                String appName = context.getPackageManager().getApplicationLabel(info).toString();
                String versionName = context.getPackageManager().getPackageInfo(info.packageName, 0).versionName;
                int installLocation = (info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0 ? 1 : 0; // 0: internal 1: sd card
                appList.add(new AppInfo(info.packageName, appName, versionName, isSystemApp, installLocation));
            } catch (Exception e) {
                CommonLogger.error("getInstalledApps error ", e);
            }
        }
        return appList;
    }

    public static class AppInfo {
        private String packageName;
        private String appName;
        private String versionName;
        private boolean isSystem;
        private int installedLocation;

        public AppInfo(String packageName, String appName, String versionName, boolean isSystem, int installedLocation) {
            this.packageName = packageName;
            this.appName = appName;
            this.versionName = versionName;
            this.isSystem = isSystem;
            this.installedLocation = installedLocation;
        }

        public String getPackageName() {
            return packageName;
        }

        public void setPackageName(String packageName) {
            this.packageName = packageName;
        }

        public String getAppName() {
            return appName;
        }

        public void setAppName(String appName) {
            this.appName = appName;
        }

        public String getVersionName() {
            return versionName;
        }

        public void setVersionName(String versionName) {
            this.versionName = versionName;
        }

        public boolean isSystem() {
            return isSystem;
        }

        public void setSystem(boolean system) {
            isSystem = system;
        }

        public int getInstalledLocation() {
            return installedLocation;
        }

        public void setInstalledLocation(int installedLocation) {
            this.installedLocation = installedLocation;
        }
    }

}
